#define  _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"

void Queue_Init(Queue* q)
{
	q->phead = NULL;
	q->phead = NULL;
	q->size = 0;
}

void Queue_Destroy(Queue* q)
{
	while (q->phead)
	{
		QNode* tmp = q->phead->next;
		free(q->phead);
		q->phead = tmp;
	}
}

void Queue_push(Queue* q,QDatatype n)
{
	assert(q);
	QNode* newNode = (QNode*)malloc(sizeof(QNode));
	if (newNode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newNode->data = n;
	newNode->next = NULL;
	if (q->phead == NULL)
	{
		q->phead = newNode;
		q->ptail = newNode;
		q->size++;
	}
	else
	{
		q->ptail->next = newNode;
		//q->ptail = q->ptail->next;
		q->ptail = newNode;
		q->size++;
	}

}

void Queue_pop(Queue* q)
{
	assert(q);
	assert(q->phead);
	/*if (q->size == 1)
	{
		free(q->phead);
		q->phead = NULL;
		q->ptail = NULL;
		q->size = 0;
	}
	else
	{
		QNode* move = q->phead;
		while (move->next != q->ptail)
		{
			move = move->next;
		}
		free(q->ptail);
		q->ptail = move;
		q->size--;
	}*/

	QNode* move = q->phead->next;
	free(q->phead);
	q->phead = move;
	q->size--;
}

QDatatype Queue_front(Queue* q)
{
	assert(q);
	assert(q->phead);
	return q->phead->data;

}

QDatatype Queue_back(Queue* q)
{
	assert(q);
	assert(q->ptail);
	return q->ptail->data;
}

int Queue_size(Queue* q)
{
	assert(q);
	return q->size;
}

bool Queue_empty(Queue* q)
{
	assert(q);
	return q->size==0;
}